package com.shemy.sshiro.controller;

import com.shemy.sshiro.domain.SysMenu;
import com.shemy.sshiro.domain.SysRole;
import com.shemy.sshiro.domain.SysUser;
import com.shemy.sshiro.service.SysMenuService;
import com.shemy.sshiro.service.SysRoleService;
import com.shemy.sshiro.service.SysUserService;
import com.shemy.sshiro.utils.JwtUtil;
import com.shemy.sshiro.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author Dzsom
 * @Date 2019-05-14 9:54
 * @Encoding UTF-8
 * @Version 1.0
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysMenuService menuService;

    @GetMapping
    @RequiresRoles("admin")
    public Result list(){
        String token = SecurityUtils.getSubject().getPrincipal().toString();
        String name = JwtUtil.getUsername(token);
        SysUser user = sysUserService.findByName(name);
        List<SysRole> roles = roleService.findByUserId(user.getUserId());
        List<SysMenu> menus = menuService.findAllByRoles(roles);
        Map<String,Object> map=new HashMap<>();
        map.put("user",user);
        map.put("roles",roles);
        map.put("menus",menus);
        return new Result(true,2000,"查询成功！",map);
    }
}
